package com.top.mall.main.mine

import com.blankj.utilcode.util.BarUtils
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewModeFragment
import com.top.mall.R
import com.top.mall.app.SPSConstance
import com.top.mall.entity.UserInfo
import com.top.mall.jetpack.vm.MineViewModel
import com.top.mall.main.home.MessageActivity
import com.top.mall.main.income.CouponCashActivity
import com.top.mall.main.income.CouponInviteActivity
import com.top.mall.popup.MineServicePopup
import kotlinx.android.synthetic.main.fragment_main_mine.*

class MineFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        fun newInstance(): MineFragment {
            return MineFragment()
        }
    }

    private val mMineServicePopup: MineServicePopup by lazy {
        MineServicePopup(requireContext()) {

        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_mine

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override fun onResume() {
        super.onResume()
        BarUtils.setStatusBarLightMode(
            requireActivity(), false
        )
    }

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMineBar)
    }

    override fun initView() {
        //为Toolbar添加状态栏高度
        BarUtils.addMarginTopEqualStatusBarHeight(flMineBar)

        SPSConstance.userInfo?.let {
            fillData(it)
        }

        tvMineAddress.setOnClickListener {
            MineAddressActivity.open()
        }

        tvMineLeader.setOnClickListener {
            MineLeaderActivity.open()
        }

        //消息
        ivMineMessage.setOnClickListener {
            MessageActivity.open(requireActivity())
        }
        //头像用户信息
        ivMineUserHead.setOnClickListener {
            MineProfileActivity.open(requireActivity())
        }

        tvMineJianmianzhengming.setOnClickListener {
            MineMzzmActivity.open()
        }

        //邀请好友
        ivMineInvitation.setOnClickListener {
            CouponInviteActivity.open(requireActivity())
        }

        //我的收藏
        linearMineCountCollect.setOnClickListener {
            MineCollectionActivity.open(requireActivity())
        }
        //代金券
        linearMineCountCoupon.setOnClickListener {
            CouponCashActivity.open(requireActivity())
        }
        //积分
        linearMineCountIntegral.setOnClickListener {
            MineIntegralActivity.open(requireActivity())
        }

        //自取点
        tvMineServicePoint.setOnClickListener {
            MineShopActivity.open(requireActivity())
        }
        //电子发票
        tvMineServiceInvoice.setOnClickListener {
            MineInvoiceActivity.open(requireActivity())
        }
        //联系客服
        tvMineServiceContact.setOnClickListener {
            mMineServicePopup.showPopupWindow()
        }
        //意见反馈
        tvMineServiceFeedback.setOnClickListener {
            MineFeedbackActivity.open(requireActivity())
        }
        //关于公司
        tvMineServiceCompany.setOnClickListener {
            MineRichTextActivity.openApi("关于公司","about")
        }
        //法律法规
        tvMineServiceLaws.setOnClickListener {
            MineRichTextActivity.openCon("法律法规","")
        }
        //设置
        tvMineServiceSetting.setOnClickListener {
            MineSettingActivity.open(requireActivity())
        }

        //商城招募
        tvMineSettlementShopping.setOnClickListener {

        }
        //入驻
        tvMineSettlementDown0.setOnClickListener {
            MineRichTextActivity.openCon("便利店入住","")
        }
        tvMineSettlementDown1.setOnClickListener {
            MineRichTextActivity.openCon("货车主入住","")
        }
        //厂家合作
        tvMineSettlementBetween.setOnClickListener {
            MineRichTextActivity.openCon("厂家合作","")
        }
    }

    override fun initData() {
        viewModel.userIndex()
    }

    override fun onRefreshLoad() {
        viewModel.userIndex()
    }

    override fun initViewMode() {
        viewModel.userIndexLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.userInfo
                ?: return@observe
            fillData(data)
        })
    }

    /**
     * 设置我的页面数据
     * */
    private fun fillData(userInfo: UserInfo) {
        ivMineUserHead.loadCircleImage(
            src = userInfo.avatar ?: ""
        )
        tvMineUserName.text = userInfo.nickname     //用户昵称
        tvMineUserMobile.text = userInfo.mobile   //用户手机号码
        tvMineUserEmail.text = userInfo.email    //用户邮箱

        tvMineCountCollect.text = userInfo.goods_collect_count.toString() //我的收藏数量
        tvMineCountCoupon.text = userInfo.cash_coupon_sum.toString()  //代金券数量
        tvMineCountIntegral.text = userInfo.score.toString()//积分数量
    }

}